package com.wanpinghui.wphlibrary.common.toast;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.wphlibrary.R;

import org.xutils.common.util.DensityUtil;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/***
 * 功能描述:带小图标的Toast
 * 作者:xiongning
 * 时间:2017/06/07
 ***/
public class RichToastDialog extends AlertDialog {

    private ImageView toast_icon;
    private TextView toast_content;
    private TextView toast_title;
    private LinearLayout mLayoutContent;

    private String content;
    private String title;
    private int resId;

    public RichToastDialog(Context context) {
        super(context);
    }

    public RichToastDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_rich_toast);
    }

    public RichToastDialog setText(String title,String content, int resId) {
        this.content = content;
        this.resId = resId;
        this.title = title;
        return this;
    }

    @Override
    public void show() {
        super.show();
        toast_icon = (ImageView) findViewById(R.id.iv_icon);
        toast_content = (TextView) findViewById(R.id.tv_content);
        toast_title = (TextView) findViewById(R.id.tv_title);
        mLayoutContent = (LinearLayout) findViewById(R.id.ll_content);
        toast_title.setText(title);
        toast_content.setText(content);
        if (resId != 0) {
            toast_icon.setVisibility(View.VISIBLE);
            toast_icon.setImageResource(resId);
        }else {
            toast_icon.setVisibility(View.GONE);
        }

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mLayoutContent.getLayoutParams();
        layoutParams.width = DensityUtil.getScreenWidth() - DensityUtil.dip2px(58);
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mLayoutContent.setLayoutParams(layoutParams);

        Observable.timer(3000, TimeUnit.MILLISECONDS, Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        dismiss();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        dismiss();
                    }
                });
    }
}


